
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';

import { HttpService } from '../../share/http/http.service';

import { Message } from './message.model';
import { NoticeService } from '../../share/service/notice.service';
import { ConfirmDialogService } from '../../share/service/confirm.service';

/**
 * grid
 */
class Grid {

    url: string;                   // 数据加载url
    columns: Array<Column>;        // 列表头
    searchBody: any;               // 查询对象
    checkBox = true;               // 是否显示表格前面的复选框
    singleSelect = true;           // 复选框是否单选
    pagination = true;             // 是否显示分页
    paginationObj: Pagination;     // 分页对象
    data: Array<any>;              // 列表数据

    constructor(
        private httpService: HttpService,
        private noticeService: NoticeService,
        private confirmDialogService: ConfirmDialogService,
        private router: Router) { }

    initOptions(options: any) {
        this.url = options.url;
        this.columns = options.columns;
        this.searchBody = options.searchBody || {};
        this.checkBox = options.checkBox === false ? false : true;
        this.singleSelect = options.singleSelect === false ? false : true;
        this.pagination = options.pagination === false ? false : true;
        this.paginationObj = new Pagination(this);
    }

    /**
     * 表格数据初始化
     * @param tHead 表格头
     * @param tBody 表格内容
     */
    initData(data: any) {
        if (data) {
            if (this.pagination) {
                this.data = data.content;
                this.paginationObj.pageCount = new Array(data.totalPages);
                this.paginationObj.pageNumber = data.number + 1;
                this.paginationObj.pageSize = data.size;
            } else {
                this.data = data;
            }
        }
    }

    /**
     * 添加
     * @param routeUrl 路由
     */
    add(routeUrl: string) {
        this.router.navigate([routeUrl, 0]);
    }

    /**
     * 删除
     * @param url http请求路径
     */
    remove(url: string) {
        this.operation(url, '确认删除吗？');
    }

    /**
     * 编辑修改
     * @param routeUrl 路由
     */
    edit(routeUrl: string) {
        let checked = this.getCheckedRowAndCheck();
        if (!checked) {
            return;
        }
        this.router.navigate([routeUrl, checked.id]);
    }

    /**
     * 操作
     * @param url http请求路径
     */
    operation(url: string, hint: string = '确认吗？') {
        let first = this.getCheckedRowAndCheck();
        if (!first) {
            return;
        }
        this.confirmDialogService.confirm({ content: hint }).subscribe(res => {
            if (res) {
                this.httpService.httpGetCallback(url + first.id, response => this.load());
            }
        });
    }

    /**
     * 选中一行发送get请求
     * @param url 请求地址
     */
    seleteOneRowSendHttpGetRequest(url: string) {
        let first = this.getCheckedRowAndCheck();
        if (!first) {
            return;
        }
        this.httpService.httpGetCallback(url + first.id, response => this.load());
    }

    /**
     * 隐藏复选框
     */
    hideCheckBox() {
        this.checkBox = false;
    }

    /**
     * 隐藏分页
     */
    hidePagination() {
        this.paginationObj.showPagination = false;
    }

    /**
     * 复选框多选
     */
    multipleSelect() {
        this.singleSelect = false;
    }

    /**
     * 获取选中的第一行
     */
    getCheckedRow(): any {
        if (!this.data) {
            return null;
        }
        let arr = this.data.filter(e => e.checked);
        return arr.length > 0 ? arr[0] : null;
    }

    /**
     * 获取所有选中的行
     */
    getCheckedRows(): any[] {
        if (!this.data) {
            return null;
        }
        let arr = this.data.filter(e => e.checked);
        return arr.length > 0 ? arr : null;
    }


    /**
     * 获取选中的第一行
     */
    getCheckedRowAndCheck(): any {
        if (!this.data) {
            return null;
        }
        let arr = this.data.filter(e => e.checked);
        if (arr.length > 0) {
            if (!arr[0].id) {
                this.noticeService.error('系统错误');
            }
            return arr[0];
        } else {
            this.noticeService.error('请选择一行');
            return null;
        }
    }

    /**
     * 当前页总行数
     */
    getRowCount() {
        return this.data && this.data.length ? this.data.length : 0;
    }

    /**
     * 当前页所有数据
     */
    getAllRow() {
        return this.data.length > 0 ? this.data : null;
    }

    /**
     * 加载数据，加载当前页数据
     */
    load() {
        this.searchBody.page = this.paginationObj.pageNumber;
        this.searchBody.size = this.paginationObj.pageSize;
        this.post(this.url, this.searchBody);
    }

    /**
     * 从第一页开始加载数据
     */
    reload() {
        this.searchBody.page = 1;
        this.searchBody.size = this.paginationObj.pageSize;
        this.post(this.url, this.searchBody);
    }


    private post(url: string, searchBody: any) {
        this.httpService.httpPostCallback(url, searchBody, (response) => {
            let msg = response as Message;
            this.initData(msg.data);
        });
    }



}

/**
 * 表格中的列
 */
class Column {
    title: string;
    field: string;
    hidden = false;
}

/**
 * 分页
 */
class Pagination {

    showPagination = true;
    pageNumber = 1;                     // 第一页，页码
    pageSize = 15;                      // 建议15
    pageCount: Array<number> = [];      // 分页空数组

    constructor(private grid: Grid) {

    }

    /**
     * 当前页
     */
    showPageNumber(index): boolean {
        if (this.pageCount.length <= 10) {
            return true;
        }
        return index !== 1 && index !== this.pageCount.length && index - 2 <= this.pageNumber && this.pageNumber <= index + 2;
    }

    /**
     * 上一页
     */
    prevPage() {
        this.pageNumber = this.pageNumber <= 0 ? 0 : --this.pageNumber;
        this.grid.load();
    }


    /**
     * 下一页
     */
    nextPage() {
        this.pageNumber = this.pageNumber >= this.pageCount.length ? this.pageCount.length : ++this.pageNumber;
        this.grid.load();

    }

    /**
     * 跳到第几页
     */
    skipToPageNumber(pageNumber: number) {
        this.pageNumber = pageNumber;
        this.grid.load();
    }

}


export {
    Grid,
    Pagination
};

